import scrapy
import bs4
from ..items import MovieBaseItem
from ..items import MovieAwardItem
from ..items import MovieCriticsItem


class DouBanSpider(scrapy.Spider):
    name = "douban"
    allowed_domains = ["movie.douban.com"]
    # start_urls = ["https://movie.douban.com/top250?start=0", "https://movie.douban.com/top250?start=25",
    #               "https://movie.douban.com/top250?start=50", "https://movie.douban.com/top250?start=75",
    #               "https://movie.douban.com/top250?start=100", "https://movie.douban.com/top250?start=125",
    #               "https://movie.douban.com/top250?start=150", "https://movie.douban.com/top250?start=175",
    #               "https://movie.douban.com/top250?start=200", "https://movie.douban.com/top250?start=250"]
    start_urls = ["https://movie.douban.com/top250?start=0"]

    def parse(self, response, **kwargs):
        bs = bs4.BeautifulSoup(response.text, 'html.parser')
        movie_list = bs.find_all(class_='item')
        for movie in movie_list:
            item = MovieBaseItem()
            item['movie_ranking'] = movie.find(class_='pic').text.strip()
            item['movie_name'] = movie.find(class_='hd').find(class_='title').text.strip()
            item['movie_info'] = movie.find(class_='bd').find('p').text.replace('\xa0', '').replace('\n', '').replace(
                '  ', '').strip()
            item['movie_score'] = movie.find(class_='star').find(class_='rating_num').text.strip()
            item['movie_comment_num'] = movie.find(class_='star').find_all('span')[3].text.strip()
            item['movie_quote'] = movie.find(class_='quote').text.strip()
            yield item
            movie_link = movie.find(class_='info').find(class_='hd').find('a')['href']
            yield scrapy.Request(movie_link, callback=self.parse_link)

    def parse_link(self, response):
        bs = bs4.BeautifulSoup(response.text, 'html.parser')
        award_link = bs.find(class_='mod').find(class_='hd').find(class_='pl').find('a')['href']
        # critic_link = bs.find(class_='reviews mod movie-content').find(class_='')
        yield scrapy.Request(award_link, callback=self.parse_award)
        yield scrapy.Request(response.url + "/reviews", callback=self.parse_critic)

    def parse_award(self, response):
        bs = bs4.BeautifulSoup(response.text, 'html.parser')
        awards_list = bs.find_all(class_='awards')
        for awards in awards_list:
            award_list = awards.find_all(class_='awards')
            for award in award_list:
                item = MovieAwardItem()
                item['awards_name'] = awards.find(class_='hd').text.strip()
                item['award_name'] = award.find_all('li')[0].text.strip()
                item['award_people'] = award.find_all('li')[1].text.strip()
                yield item

    def parse_critic(self, response):
        bs = bs4.BeautifulSoup(response.text, 'html.parser')
        review_list = bs.find_all(class_='main review-item')
        for review in review_list:
            item = MovieCriticsItem()
            item['critic_name'] = review.find('header').find(class_='name').text.strip()
            item['critic_score'] = review.find('header').find(class_='main-title-rating')['title']
            item['critic_time'] = review.find('header').find(class_='main-meta').text.strip()
            item['critic_title'] = review.find(class_='main-bd').find('h2').text.strip()
            item['critic_content'] = review.find(class_="main-bd").find(class_='review-short').text.strip()
            yield item
